package com.woniuxy.joylifemarket.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.common.entity.ShopAddress;
import com.woniuxy.common.entity.ShopUserAddress;
import com.woniuxy.joylifemarket.service.CenterUserService;
import com.woniuxy.joylifemarket.service.ShopAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AddressController {
    @Autowired
    ShopAddressService shopAddressService;
    @Autowired
    CenterUserService centerUserService;

    //初始化全部地址
    @RequestMapping("/initalladdress")
    public Object userAddress(String userId) {
            Map<String, Object> result = new HashMap<>();
            try {
                List<ShopUserAddress> allAddressByUserId = shopAddressService.findAllAddressByUserId(userId);
                result.put("code", 200);
                result.put("msg", "查询成功");
                result.put("data", allAddressByUserId);
//            System.out.println("allAddressByUserId"+allAddressByUserId);
                return result;
            } catch (Exception e) {
                result.put("code", 500);
                result.put("msg", "查询失败");
                e.printStackTrace();
                return result;
            }
    }

    //初始化默认地址
    @RequestMapping("/initdefaultaddress")
    public Object userDefaultAddress(String userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            ShopUserAddress defaultAddressByUserId = shopAddressService.findDefaultAddressByUserId(userId);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", defaultAddressByUserId);
//            System.out.println(defaultAddressByUserId);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //新增地址
    @RequestMapping("/addaddress")
    public Object addAddress(String address,String userAddress) {
//        System.out.println("address："+address+"，userAddress："+userAddress);
        ShopAddress shopAddress = JSON.parseObject(address, ShopAddress.class);
        ShopUserAddress shopUserAddress = JSON.parseObject(userAddress, ShopUserAddress.class);
//        System.out.println("address："+shopAddress+"，userAddress："+shopUserAddress);
        Map<String, Object> result = new HashMap<>();
        try {
            int i = shopAddressService.addAddressByInfo(shopAddress, shopUserAddress);
            if (i==1) {
                //插入成功，返回成功码
                result.put("code", 200);
                result.put("msg", "查询成功");
                return result;
            }
            result.put("code", 500);
            result.put("msg", "查询失败");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }
}
